#!/usr/bin/env php
<?php
/**
 * Introduce:
 *
 * The `compiler.jar` download for https://github.com/google/closure-compiler, The Plugins used of Js compress and merge.
 *  Command params:
 *  --js [[Specifies the JS file to be compressed]]
 *  --js_output_file [[The compressed JS file name and position]]
 *  --create_source_map [[File name and location of the compressed SourceMap]]
 *  --source_map_format [[SourceMap version]]
 *
 * The `yuicompressor.jar` download for https://github.com/yui/yuicompressor, The Plugins used of Css compress and merge.
 *
 * Dependent Java environment, The java installation address:
 * https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
 *
 * Console execute:
 * js:
 * pack --js --from=./builder/assets/angular-select2-ui/select2.js --to=./builder/assets/angular-select2-ui/select2.min.js --map=./builder/assets/angular-select2-ui/select2.min.js.map
 *
 * css:
 * pack --css --from=./builder/assets/select24/css/select2.css --to=./builder/assets/select24/css/select2.min.css
 */

$command = [
    'js' => 'java -jar compiler.jar --js [--from] --js_output_file [--to] --create_source_map [--map]',
    'css' => 'java -jar yuicompressor.jar --type css [--from] -o [--to]',
];

$params = [];
foreach ($_SERVER['argv'] as $exp) {
    parse_str($exp, $arr);
    $params = array_merge($params, $arr);
}

if (key_exists('--css', $params)) {
    $executeCommand = $command['css'];
} elseif (key_exists('--js', $params)) {
    $executeCommand = $command['js'];
} else {
    $executeCommand = '';
}

$replaceParams = [];
if (!empty($executeCommand)) {
    foreach ($params as $key => $val) {
        $replaceParams["[{$key}]"] = $val;
    }

    $executeCommand = strtr($executeCommand, $replaceParams);

    $result = exec($executeCommand);
    echo $result;

    exit(0);
}

echo 'Please select command.';

